<?php
/**
 * Joomla! 1.5 component NS Pro
 *
 * @version $Id: install.nspro.php 2009-04-28 01:47:12 svn $
 * @author Christopher Mavros
 * @package Joomla
 * @subpackage NS Pro
 * @license GNU/GPL
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Initialize the database
$db =& JFactory::getDBO();
$update_queries = array (
'CREATE TABLE IF NOT EXISTS `#__nspro_subs` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `email` varchar(200) NOT NULL,
  `datetime` DATETIME NOT NULL,
  `notes` TEXT NULL,
  `confirmed` BOOL NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletters` (
  `id` int(11) NOT NULL auto_increment,
  `fName` varchar(100) NOT NULL,
  `fEmail` varchar(200) NOT NULL,
  `subject` TEXT NULL,
  `message` TEXT NULL,
  `datetime` DATETIME NOT NULL,
  `sent` BOOL NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_lists` (
  `id` int(11) NOT NULL auto_increment,
  `lname` varchar(100) NOT NULL,
  `published` BOOL NOT NULL,
  `notes` TEXT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletter_attachments` (
  `id` int(11) NOT NULL auto_increment,
  `newsletter_id` int(11) NOT NULL,
  `filename` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletter_openings` (
  `id` int(11) NOT NULL auto_increment,
  `nid` int(11) NOT NULL,
  `openings` int(11) NOT NULL,
  `email` varchar(200) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_html_templates` (
  `id` int(11) NOT NULL auto_increment,
  `tname` varchar(200) NOT NULL,
  `tcode` TEXT NOT NULL,
  `thumbpath` varchar(500) NULL,
  `notes` TEXT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_fields` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(200) NOT NULL,
  `fieldtype` int(11) NOT NULL,
  `regexp` varchar(300) NULL,
  `required` BOOL NOT NULL,
  `published` BOOL NOT NULL,
  `show_backend` BOOL NOT NULL,
  `value` TEXT NULL,
  `ordering` int(11) NOT NULL,
  `notes` TEXT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_field_values` (
  `id` int(11) NOT NULL auto_increment,
  `value` TEXT NOT NULL,
  `subscriber_id` int(11) NOT NULL,
  `field_id` int(11) NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;');

$alternative_queries = array (
'CREATE TABLE IF NOT EXISTS `#__nspro_subs` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `email` varchar(200) NOT NULL,
  `datetime` DATETIME NOT NULL,
  `notes` TEXT NULL,
  `confirmed` BOOL NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletters` (
  `id` int(11) NOT NULL auto_increment,
  `fName` varchar(100) NOT NULL,
  `fEmail` varchar(200) NOT NULL,
  `subject` TEXT NULL,
  `message` TEXT NULL,
  `datetime` DATETIME NOT NULL,
  `sent` BOOL NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_lists` (
  `id` int(11) NOT NULL auto_increment,
  `lname` varchar(100) NOT NULL,
  `published` BOOL NOT NULL,
  `notes` TEXT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletter_attachments` (
  `id` int(11) NOT NULL auto_increment,
  `newsletter_id` int(11) NOT NULL,
  `filename` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletter_openings` (
  `id` int(11) NOT NULL auto_increment,
  `nid` int(11) NOT NULL,
  `openings` int(11) NOT NULL,
  `email` varchar(200) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_html_templates` (
  `id` int(11) NOT NULL auto_increment,
  `tname` varchar(200) NOT NULL,
  `tcode` TEXT NOT NULL,
  `thumbpath` varchar(500) NULL,
  `notes` TEXT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_fields` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(200) NOT NULL,
  `fieldtype` int(11) NOT NULL,
  `regexp` varchar(300) NULL,
  `required` BOOL NOT NULL,
  `published` BOOL NOT NULL,
  `show_backend` BOOL NOT NULL,
  `value` TEXT NULL,
  `ordering` int(11) NOT NULL,
  `notes` TEXT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_field_values` (
  `id` int(11) NOT NULL auto_increment,
  `value` TEXT NOT NULL,
  `subscriber_id` int(11) NOT NULL,
  `field_id` int(11) NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;');

$additional_queries = array (
'ALTER TABLE `#__nspro_subs` ADD COLUMN `mailing_lists` varchar(60) NOT NULL;',
'ALTER TABLE `#__nspro_newsletters` ADD COLUMN `mailing_lists` varchar(60) NOT NULL;',
'ALTER TABLE `#__nspro_newsletters` ADD COLUMN `emails_per_send` INT(11) NOT NULL;',
'ALTER TABLE `#__nspro_newsletters` ADD COLUMN `LastID` INT(11) NOT NULL;',
'ALTER TABLE `#__nspro_fields` ADD COLUMN `abovepos` BOOL NOT NULL;',
'ALTER TABLE `#__nspro_newsletters` ADD COLUMN `is_scheduled` int(11) NOT NULL;',
'ALTER TABLE `#__nspro_newsletters` ADD COLUMN `scheduledatetime` DATETIME NOT NULL;');

// Perform all queries - we dont care if it fails
$try_alternatives = false;
foreach( $update_queries as $query ) {
    $db->setQuery( $query );
    if (!$db->query()) {
      $try_alternatives = true;
    }
}

if ($try_alternatives) {
  foreach( $alternative_queries as $query ) {
      $db->setQuery( $query );
      if (!$db->query()) {
        print $db->getErrorMsg();
      }
  }
}

foreach( $additional_queries as $query ) {
    $db->setQuery( $query );
    if (!$db->query()) {
      //print $db->getErrorMsg();
      // I would print errors, but this always tries to insert duplicate columns, so... :)
    }
}

$tquery = 'SELECT COUNT(`id`) FROM `#__nspro_html_templates`';
$db->setQuery($tquery);
$result = $db->loadResult();

if ($result == 0) {
  //insert some templates

  // SUNNY TEMPLATE
  $tname = 'NS Pro Sunny Template';
  $bas = &JURI::base();
  $thumbpath = $bas . 'components/com_nspro/uploads/newsletters/sunnytemplate.png';
  $imgpath = $bas . 'components/com_nspro/uploads/newsletters/';
  $tcode = '<table width="100%" height="100%" style="background-color: #85bbe2; font-family: Verdana, Tahoma, Sans-Serif;" cellspacing="0" cellpadding="0" border="0">
<tr><td align="center" valign="top">
<table cellspacing="0" cellpadding="0" border="0" width="630" style="width: 630px;">
<tr><td background="' . $imgpath . 'top.png" height="50" style="font-size: 9px; text-align: center;"><br/>Not Viewing Properly? See the {link:NEWSLETTER}Online Version{/link}</td></tr>
<tr><td background="' . $imgpath . 'header.png" height="78" style="font-size: 32px; padding: 0 0 0 40px;">Your Dream Vacations</td></tr>
<tr><td background="' . $imgpath . 'content.png" style="background: #fff url(' . "'" . $imgpath . 'content.png' . "'" . ') repeat-y center top; padding: 10px;">
<table cellspacing="0" cellpadding="10" border="0" style="font-size: 13px;">
<tr><td valign="top"><img src="'. $imgpath . 'beach.jpg" style="border: 1px solid #000;"/></td><td><b>Dear {subscriber_name}</b>,<br/>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent sapien tortor, congue ut aliquam sit amet, scelerisque non magna. Morbi in dui ut quam blandit ultricies vel quis ligula. Duis bibendum ullamcorper vehicula. Ut nec erat ante. Vestibulum pellentesque volutpat massa, pellentesque auctor augue eget. Vivamus adipiscing est in nibh gravida luctus. Quisque vitae lacus in magna scelerisque faucibus. In dumus posci vel dui pulvinar a rutrum neque suscipit.</td></tr>
<tr>
<td>Morbi at cursus mi. Suspendisse auctor turpis at erat tempor mollis. Nullam gravida vestibulum convallis. Mauris aliquam erat sit amet erat scelerisque vel congue nisi aliquam. In luctus sapien justo, quis convallis elit. Sed nisl nibh, facilisis sed blandit nec, sagittis ac erat. Phasellus mollis orci quis tortor sagittis sollicitudin. In venenatis luctus eros, in ultrices est facilisis quis. Skitalis brutus afnomit nisi convallis. Nulla volutpat consectetur metus, eget hendrerit massa sollicitudin sed. Sed fini, kameris oto.</td>
<td>Vestibulum laoreet eleifend congue. Vivamus pretium risus non orci lacinia convallis. Vivamus congue adipiscing risus, a molestie quam bibendum quis. Maecenas tempus tellus elementum lectus laoreet facilisis a eget tellus. Ut hendrerit auctor augue sit amet ultricies. Cras tempus odio et ante suscipit blandit nec eget metus. Integer quis lectus est.</td>
</tr>
</table>
</td></tr>
<tr><td background="' . $imgpath . 'footer.png" height="112">
  <table cellspacing="0" cellpadding="0" border="0" width="100%">
    <tr><td height="56" style="font-size: 14px;" align="center" valign="top">Your Company Name, Your Company Address<br/>Your Name, Your Position, email@yoursite.com</td></tr>
    <tr><td style="font-size: 9px;" align="center" valign="bottom">To ensure you get our latest news, add email@yoursite.com to your address book.<br/>If you do not want to receive further information from OurSite.com, {link:UNSUB}unsubscribe here{/link}.</td></tr>
  </table>
</td></tr>
<tr><td background="' . $imgpath . 'bottom.png" height="50">&nbsp;</td></tr>
</table>
</td></tr>
</table>';
  $notes = 'The Sunny Template, simple and elegant. Ideal for vacation or hotel newsletters. Content Structure: Image - Column, or 2 columns.';
  $iquery = 'INSERT INTO `#__nspro_html_templates` (`tname`, `thumbpath`, `tcode`, `notes`) VALUES ("' .
            $tname . '", "' . $thumbpath . '", "' . mysql_escape_string($tcode) . '", "' . $notes . '")';
  $db->setQuery($iquery);
  $db->query();

  // NEW STYLISH TEMPLATE
  $tname = 'NS Pro New Stylish Template';
  $bas = &JURI::base();
  $thumbpath = $bas . 'components/com_nspro/uploads/newsletters/newstylish.png';
  $imgpath = $bas . 'components/com_nspro/uploads/newsletters/';
  $tcode = '<table border="0" cellspacing="0" cellpadding="0" bgcolor="#FFCCFF" width="100%">
<tr><td align="center">
<table border="0" cellspacing="0" cellpadding="0" width="600"><tr>
<td background="'.$imgpath.'bg.gif" align="center" style="padding: 50px;">
<table border="0" cellspacing="0" cellpadding="0" width="500">
<tr><td background="'.$imgpath.'top.gif" height="140" align="center">
<a href="#yoursite.com" target="_blank"><img src="'.$imgpath.'logo.png" border="0" alt="New Stylish Template"/></a>
</td></tr>
<tr><td bgcolor="#66001A" style="padding: 0 20px;">
<table border="0" cellspacing="0" cellpadding="0" width="460">
<tr><td colspan="2" align="center">
  <img src="'.$imgpath.'slide.jpg" alt="Urban Dancer" style="border: 2px solid #330000"/>
</td></tr>
<tr>
<td align="left" valign="top" width="230" style="padding: 10px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed ac nisi eget felis congue aliquet vitae et nunc.
Sed sollicitudin augue eget tortor lacinia iaculis.
Class aptent taciti sociosqu ad litora torquent per conubia nostra,
per inceptos himenaeos.
Curabitur fermentum nisi a purus ornare ultrices posuere libero tincidunt.
Cras gravida lobortis pharetra. Phasellus eget orci lectus, interdum imperdiet nulla.
Sed ac quam ligula.</td>
<td align="left" valign="top" width="230" style="padding: 10px;">Curabitur fermentum nisi a purus ornare ultrices posuere libero tincidunt.
Cras gravida lobortis pharetra. Phasellus eget orci lectus, interdum imperdiet nulla.
Sed ac quam ligula. Phasellus mi erat, sodales et interdum eget, accumsan nec metus.
Suspendisse non nibh arcu, non facilisis orci. Etiam convallis eros non sapien rutrum dapibus.
Donec ante tellus, tempus a volutpat non, elementum quis enim.</td>
</tr>
</table>
</td></tr>
<tr><td background="'.$imgpath.'bottom.gif" align="center" height="110" style="font-family: Verdana, Sans-Serif; font-size: 12px; color: #ff9999;">
<p>Your Company Name, YourCompanySite.com</p>
<p>your@email.com, 1-555-1234</p>
<p><a href="{url:UNSUB}" target="_blank" style="color: #ff9999;">Unsubscribe here</a></p>
</td></tr>
</table>
</td></tr></table>
</td></tr></table>';
  $notes = 'The New Stylish NS Pro Template. An urban newsletter, combining some classic and stylish flower shapes, in burgundy colors. Content Structure: Single Image - 2 Columns.';
  $iquery = 'INSERT INTO `#__nspro_html_templates` (`tname`, `thumbpath`, `tcode`, `notes`) VALUES ("' .
            $tname . '", "' . $thumbpath . '", "' . mysql_escape_string($tcode) . '", "' . $notes . '")';
  $db->setQuery($iquery);
  $db->query();
}

?>